<?php
namespace Admin\Controller;
use Think\Controller;
class ListinfoController extends CommonController {
     public function lists(){
		$listinfo = M("listinfo");
		$list = $listinfo->query("select * from zwp_listinfo");
		foreach($list as $k=>$v){
			$count = substr_count($v['pid'],",");
			$list[$k]['order']=$count;
			$list[$k]["title"]= $this->kong($count).$v['title'];
		}
		$this->assign("lists",$list);
		if($_REQUEST["title"]){
			$title = $_POST["title"];
			$listinfo = M('listinfo'); // 实例化User对象
			$numPerPage = empty($_REQUEST["numPerPage"])?2:$_REQUEST["numPerPage"];
			$count  = $listinfo->where("title = '$title'")->count();// 查询满足要求的总记录数
			$Page  = new \Think\Page($count,$numPerPage);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show  = $Page->show();// 分页显示输出
			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$list = $listinfo->where("title = '$title'")->order("id desc")->limit($Page->firstRow.','.$Page->listRows)->select();
			//当前数据
			$this->assign("lists",$list);
			//总条数
			$this->assign("totalCount",$count);
			//每页显示多少条
			$this->assign("numPerPage",$numPerPage);
			//页标数字多少个
			$this->assign("pageNumShown",$Page->totalPages);
			//当前显示第几页
			$page = empty($_REQUEST["pageNum"])?1:$_REQUEST["pageNum"];
			$this->assign("currentPage",$page);
			$this->assign("title",$title);
		}else{
			$listinfo = M('listinfo'); // 实例化User对象
			$numPerPage = empty($_REQUEST["numPerPage"])?2:$_REQUEST["numPerPage"];
			$count  = $listinfo->count();// 查询满足要求的总记录数
			$Page  = new \Think\Page($count,$numPerPage);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show  = $Page->show();// 分页显示输出
			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$list = $listinfo->order("id desc")->limit($Page->firstRow.','.$Page->listRows)->select();
			//当前数据
			$this->assign("lists",$list);
			//总条数
			$this->assign("totalCount",$count);
			//每页显示多少条
			$this->assign("numPerPage",$numPerPage);
			//页标数字多少个
			$this->assign("pageNumShown",$Page->totalPages);
			//当前显示第几页
			$page = empty($_REQUEST["pageNum"])?1:$_REQUEST["pageNum"];
			$this->assign("currentPage",$page);
		}
		$this->display();
	}
	public function kong($a){
		$str = "";
		for($i=0;$i<$a;$i++){
			$str .= "&nbsp;---|";
		}
		return $str;
	}
	public function add(){
		$listinfo = M("fenlei");
		$shuju = $listinfo->select();
		$tuijian = M("recommend")->select();
		$this->assign("tuijian",$tuijian);
		$this->assign("shuju",$shuju);
		$this->display();
	}
	public function photo(){
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =     3145728 ;// 设置附件上传大小
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->rootPath  =      $this->paths(); // 设置附件上传根目录
		$upload->savePath  =      ''; // 设置附件上传（子）目录
		// 上传文件 
		$info   =   $upload->upload();
		return $info;
	}
	public function paths(){
		$path='./public/Uploads/';
		return $path;
	}

	public function addAction(){
		$photo = $this->photo();
		$arr = $_POST['recommend_id'];
		$str = implode(",",$arr);
		$_POST["recommend_id"] = $str;
		$_POST["photo"] = $photo['photo']['savename'];
		$sql = M("listinfo")->add($_POST); 
		//判断
		if($sql){
			//查看数组个数
			if(count($arr)>0){
				foreach($arr as $k=>$v){
					$data["pid"] = $sql;
					$data["tid"] = $v;
					$data["name"] = $_POST["title"];
					M("tuijianlist")->add($data);
				}
			}
			$this->success("添加成功");
		}else{
			$this->error("添加失败");
		} 
	}
	public function edit(){
		$id = $_GET["uid"];
		//查询推荐类型表
		$info = M("recommend")->select();
		$this->assign("info",$info);
		//查询商品详情列表
		$result = M("listinfo")->where("id=$id")->find();//查询一条数据
		//var_dump($result);
		$this->assign("result",$result);
		$this->display();
	}
	public function editAction(){
		$id = $_POST["id"];
		//取到多选框的值
		$recname = $_POST["choose"];
		$str = implode(",",$recname);
		if(!empty($str)){
			//存在时添加到listinfo表里面
			$_POST["recommend_id"] = $str;
			
		}else{
			$_POST["recommend_id"] = "";
			$sql = M("tuijianleilist")->where("pid=$id")->delete();
		}
		$info = M("listinfo")->save($_POST);

		if($info){
			if(!empty($str)){
				//删除掉tuijianleilist里的数据
				$sql = M("tuijianleilist")->where("pid=$id")->delete();
					foreach($recname as $k => $v){
						$data["pid"] = $id;
						$data["tid"] = $v;
						$data["name"] = $_POST["title"];
						$reinfo = M("tuijianleilist")->add($data);
					}
					
				}
				
			$this->success("修改成功");
		}else{
			$this->error("修改失败");
		}
	}
	public function del(){
		$id = $_GET["uid"];
		$sql = M("listinfo")->where("id=$id")->delete($_POST);
		if($sql){
			$shanchu = M("tuijianlist")->where("pid = '$id'")->delete();
			$this->success("删除成功");
		}else{
			$this->error("删除失败");
		}
	}
}